package com.linkage.seapeak.activity.project;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.activity.Main.PDFViewActivity;
import com.linkage.jz.bankruptcy.adapter.FullyGridLayoutManager;
import com.linkage.jz.bankruptcy.bean.TaskAtta;
import com.linkage.jz.bankruptcy.utils.Utils;
import com.linkage.seapeak.base.BaseWhiteCreateActivity;
import com.linkage.seapeak.pojo.UploadImage;
import com.linkage.seapeak.utils.CommonUtils;
import com.seapeak.recyclebundle.BaseHolder;
import com.seapeak.recyclebundle.BaseRecyclerAdapter;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class DissentShowActivity extends BaseWhiteCreateActivity {

    private TextView adversText;
    private RecyclerView recycler_pdf_file;
    private RecyclerView recyclerShowImgs;
    private List<TaskAtta> pdfList = new ArrayList<>();
    private List<UploadImage> imageListShow = new ArrayList<>();
    private GridImagesAdapter imgAdapterShow;
    private TextView advTime;

    private ItemData mInfo;
    private int type;

    private TextView appendText;
    private RecyclerView appendPdfFile;
    private RecyclerView appendShowImgs;
    private List<TaskAtta> appendPdfList = new ArrayList<>();
    private List<UploadImage> appendImageListShow = new ArrayList<>();
    private GridImagesAdapter appendAdapterShow;
    private TextView appendTime;

    public static void start(Context context, String infoString, int type) {
        Intent intent = new Intent(context, DissentShowActivity.class);
        intent.putExtra("infoString", infoString);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dissent_show, "");
        mInfo = new Gson().fromJson(getIntent().getStringExtra("infoString"), ItemData.class);
        type = getIntent().getIntExtra("type", 0);
        setTitle(type == 0 ? "查看异议信息" : "查看异议回复");
        initView();

        advTime.setText(CommonUtils.resetTime(mInfo.opinionTime * 1000L));
        adversText.setText(mInfo.opinion);

        pdfList.clear();
        imageListShow.clear();
        if (mInfo.questionAttaList != null && mInfo.questionAttaList.size() > 0) {
            for (int i = 0; i < mInfo.questionAttaList.size(); i++) {
                TaskAtta atta = mInfo.questionAttaList.get(i);
                if (atta.getAttachmentSuffix().equals("pdf")) {
                    pdfList.add(atta);
                    recycler_pdf_file.getAdapter().notifyDataSetChanged();
                } else {
                    JSONObject obj = new JSONObject();
                    try {
                        obj.putOpt("filename", atta.getAttachmentName());
                        obj.putOpt("filepath", atta.getAttachmentDownloadAddress());
                        obj.putOpt("filesize", atta.getAttachmentSize());
                        obj.putOpt("loadUrl", atta.getAttachmentWatermarkAddress());
                        imageListShow.add(new Gson().fromJson(obj.toString(), UploadImage.class));
                        imgAdapterShow.notifyDataSetChanged();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        findViewById(R.id.append_layout).setVisibility(type == 0 ? View.GONE : View.VISIBLE);
        if (type == 1 ) {
            appendPdfList.clear();
            appendImageListShow.clear();
            appendText.setText(mInfo.responses);
            appendTime.setText(CommonUtils.resetTime(mInfo.responsesTime * 1000L));
            for (int i = 0; i < mInfo.responsesAttaList.size(); i++) {
                TaskAtta atta = mInfo.responsesAttaList.get(i);
                if (atta.getAttachmentSuffix().equals("pdf")) {
                    appendPdfList.add(atta);
                    appendPdfFile.getAdapter().notifyDataSetChanged();
                } else {
                    JSONObject obj = new JSONObject();
                    try {
                        obj.putOpt("filename", atta.getAttachmentName());
                        obj.putOpt("filepath", atta.getAttachmentDownloadAddress());
                        obj.putOpt("filesize", atta.getAttachmentSize());
                        obj.putOpt("loadUrl", atta.getAttachmentWatermarkAddress());
                        appendImageListShow.add(new Gson().fromJson(obj.toString(), UploadImage.class));
                        appendAdapterShow.notifyDataSetChanged();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    private void initView() {
        adversText = (TextView) findViewById(R.id.advers_text);
        recyclerShowImgs = (RecyclerView) findViewById(R.id.recycler_show_imgs);
        recycler_pdf_file = (RecyclerView) findViewById(R.id.recycler_pdf_file);
        advTime = (TextView) findViewById(R.id.adv_time);

        appendText = (TextView) findViewById(R.id.append_text);
        appendPdfFile = (RecyclerView) findViewById(R.id.append_pdf_file);
        appendShowImgs = (RecyclerView) findViewById(R.id.append_show_imgs);
        appendTime = (TextView) findViewById(R.id.append_time);

        /**
         * PDF 的适配器展示
         */
        recycler_pdf_file.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,
                false));
        recycler_pdf_file.setAdapter(new BaseRecyclerAdapter<PdfHolder>() {
            @NonNull
            @Override
            public PdfHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
                return new PdfHolder(LayoutInflater.from(DissentShowActivity.this).inflate(R.layout.item_only_text, viewGroup, false));
            }

            @Override
            public void onBindViewHolder(@NonNull PdfHolder viewHolder, int i) {
                TaskAtta item = pdfList.get(i);
                viewHolder.itemText.setText(item.getAttachmentName());
                viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(DissentShowActivity.this, PDFViewActivity.class);
                        intent.putExtra("url", item.getAttachmentWatermarkAddress());
                        startActivity(intent);
                    }
                });
            }

            @Override
            public int getItemCount() {
                return pdfList == null ? 0 : pdfList.size();
            }
        });
        recyclerShowImgs.setLayoutManager(new FullyGridLayoutManager(this, 4, GridLayoutManager.VERTICAL,
                false));
        imgAdapterShow = new GridImagesAdapter(this, TYPE_PREVIEW);
        imgAdapterShow.setList(imageListShow);
        recyclerShowImgs.setAdapter(imgAdapterShow);


        if (type == 1) {
            /**
             * PDF 的适配器展示
             */
            appendPdfFile.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,
                    false));
            appendPdfFile.setAdapter(new BaseRecyclerAdapter<PdfHolder>() {
                @NonNull
                @Override
                public PdfHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
                    return new PdfHolder(LayoutInflater.from(DissentShowActivity.this).inflate(R.layout.item_only_text, viewGroup, false));
                }

                @Override
                public void onBindViewHolder(@NonNull PdfHolder viewHolder, int i) {
                    TaskAtta item = appendPdfList.get(i);
                    viewHolder.itemText.setText(item.getAttachmentName());
                    viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(DissentShowActivity.this, PDFViewActivity.class);
                            intent.putExtra("url", item.getAttachmentWatermarkAddress());
                            startActivity(intent);
                        }
                    });
                }

                @Override
                public int getItemCount() {
                    return appendPdfList == null ? 0 : appendPdfList.size();
                }
            });
            appendShowImgs.setLayoutManager(new FullyGridLayoutManager(this, 4, GridLayoutManager.VERTICAL,
                    false));
            appendAdapterShow = new GridImagesAdapter(this, TYPE_PREVIEW);
            appendAdapterShow.setList(appendImageListShow);
            appendShowImgs.setAdapter(appendAdapterShow);
        }

    }

    class PdfHolder extends BaseHolder {

        TextView itemText;

        public PdfHolder(@NonNull View itemView) {
            super(itemView);
            itemText = itemView.findViewById(R.id.item_text);
        }
    }

    class ItemData {
        /**
         * id : bc275e7715db48e0918770d9c5efb1cb
         * bankruptcyProjectId : fe459c6944884929b5d35e82f4d51077
         * creditorRightReviewId : 602cc5557ef94d1f9bef4add487c2571
         * title : null
         * opinion : ヾ(❀╹◡╹)ﾉ~略略略
         * creditorId : 0cbc1d4da4a24ec4a3216c4d7a94ec8f
         * opinionTime : 1559575129
         * responses : null
         * responsesStatus : false
         * memberId : null
         * responsesTime : null
         * questionAttaList : []
         * responsesAttaList : []
         * opBy : 0720d92fdaf24411b94a197da3eeb814
         * opAt : 1559575129
         * delFlag : false
         */

        public String id;
        public String bankruptcyProjectId;
        public String creditorRightReviewId;
        public String title;
        public String opinion;
        public String creditorId;
        public long opinionTime;
        public String responses;
        public boolean responsesStatus;
        public String memberId;
        public long responsesTime;
        public String opBy;
        public long opAt;
        public boolean delFlag;
        public List<TaskAtta> questionAttaList;
        public List<TaskAtta> responsesAttaList;
    }
}
